<?php

/**
 * Admin_Monitor_Capacity Model
 * 
 * Short file description
 *
 * PHP Version: 5.3.26
 * 
 * @category Admin_Monitor_Capacity_Models
 * @package  Admin_Monitor_Capacity_Package
 * @author   Johari <joharijumali@gmail.com>
 */

namespace Admin\Models\Monitor;

use \Laravel\Database\Eloquent\Model as Eloquent;

/**
 * Class Admin_Monitor_Capacity Model
 * 
 * Class Description
 *
 * @category Admin_Monitor_Capacity_Models
 * @package  Admin_Monitor_Capacity_Package
 * @author   Johari <joharijumali@gmail.com>
 */

class Capacity extends Eloquent 
{

	public static $timestamps = true;
    public static $table = 'server_disk';
    public static $key = 'disk_id';

	/**
     * Function cron that pass parameter
     *
     * @param integer $id   the integer to quote
     * @param varchar $data the varchar to quote
     * @param varchar $date the varchar to quote
	 *
	 * @return parameter.
     */
    public static function cron($id, $data, $date)
	{	
        $data = json_decode($data);
        
        $exist = Capacity::where('server_id', '=', $id)->first(array('disk_id'));

    	$Capacity = (empty($exist))? new Capacity : Capacity::find($exist->disk_id);

        $Capacity->server_id = $id;
        $Capacity->disk_total = $data->total;
        $Capacity->disk_free = $data->free;
        $Capacity->disk_date = $date;
        $Capacity->save();
        



    }
	

    /**
     * Function grabSingleLoad that retrive parameter
     *
     * @param integer $server_id   tserver id
     *
     * @return parameter.
     */
    public static function grabSingleCapacity($server_id)
    {
        
        $Capacity = Capacity::where('server_id', '=', $server_id)->first()->to_array();

        return $Capacity;
    }


    /**
     * Function grabSingleLoad that retrive parameter
     *
     * @param integer $server_id   tserver id
     *
     * @return parameter.
     */
    public static function grabAllCapacity()
    {
        
        $Capacity = Capacity::all();

        $data = array();

        foreach ($Capacity as $key => $value) {
            $data[$value->server_id][$key] = $value->to_array();
            
            unset($data[$value->server_id][$key]['created_at']);
            unset($data[$value->server_id][$key]['updated_at']);
            unset($data[$value->server_id][$key]['server_id']);
            unset($data[$value->server_id][$key]['disk_id']);
        }

        return $data;
    }

}
